package com.tledu.zyf.servlet.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tledu.zyf.model.Group;
import com.tledu.zyf.model.User;
import com.tledu.zyf.util.ObjectFactory;

@WebServlet("/user/add")
public class UserAddServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取传递的数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String nickname = request.getParameter("nickname");
		int age = Integer.parseInt(request.getParameter("age"));
		int sex = Integer.parseInt(request.getParameter("sex"));
		int group_id = Integer.parseInt(request.getParameter("group_id"));
		Group group = null;
		// 如果为 0 ,说明中没有部门
		if (group_id != 0) {
			group = new Group();
			group.setId(group_id);
		}
		User user = new User(username, password, nickname, sex, age);
		user.setGroup(group);
		// System.out.println(user);
		ObjectFactory.getUserService().add(user);
	}
}
